package com.canaan.business.schedule;

import cn.hutool.core.thread.ThreadUtil;
import com.canaan.business.common.enums.agv.AgvEnum;
import com.canaan.business.domain.agv.AgvTaskDispatch;
import com.canaan.business.service.agv.IAgvTaskDispatchService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Slf4j
@Component("AgvTaskSchedule")
public class AgvTaskSchedule {

    @Resource
    private IAgvTaskDispatchService taskDispatchService;


    /**
     * 每隔8s向rcs发送任务命令
     */
    @ApiOperation(value = "发送任务", notes = "发送任务")
    public void sendTaskInstruction() {
        try {
            //todo 要根据每种agv数量发送任务
            AgvTaskDispatch queryParam = new AgvTaskDispatch();
            queryParam.setTaskStatus(AgvEnum.AGV_TASK_STATUS.SENDING.getCode());
            List<AgvTaskDispatch> dispatchList = taskDispatchService.selectAgvTaskDispatchList(queryParam);
            if (CollectionUtils.isEmpty(dispatchList)) {
                return;
            }
            for (AgvTaskDispatch taskDispatch : dispatchList) {
                if (Objects.nonNull(taskDispatch)) {
                    taskDispatchService.sendSchedulingTaskByAgvDispatch(taskDispatch);
                }
                ThreadUtil.sleep(500);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            log.error(e.toString());
        }
    }
}
